{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# lambda函数用法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "p =  10\n"
     ]
    }
   ],
   "source": [
    "# 例1:传入多个参数的lambda函数\n",
    "\n",
    "def sum(x,y):\n",
    "    return x + y\n",
    "\n",
    "\n",
    "# 用lambda来实现：\n",
    "\n",
    "p = lambda x,y: x + y\n",
    "print('p = ',p(3,7))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n"
     ]
    }
   ],
   "source": [
    "# 例2：传入一个参数的lambda函数\n",
    "\n",
    "a = lambda x: x * x\n",
    "print(a(3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a =  13\n"
     ]
    }
   ],
   "source": [
    "# 例3：多个参数的lambda形式：\n",
    "\n",
    "a = lambda x,y,z:(x+8)*y - z\n",
    "print('a = ',a(1,2,5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "\n",
    "匿名函数lambda：是指一类无需定义标识符（函数名）的函数或子程序。\n",
    "lambda 函数可以接收任意多个参数 (包括可选参数) 并且返回单个表达式的值。\n",
    "\n",
    "要点：\n",
    "1，lambda 函数不能包含命令，\n",
    "\n",
    "2，包含的表达式不能超过一个。\n",
    "\n",
    "说明：一定非要使用lambda函数；任何能够使用它们的地方，都可以定义一个单独的普通函数来进行替换。我将它们用在需要封装特殊的、非重用代码上，避免令我的代码充斥着大量单行函数。\n",
    "\n",
    "lambda匿名函数的格式：冒号前是参数，可以有多个，用逗号隔开，冒号右边的为表达式。其实lambda返回值是一个函数的地址，也就是函数对象。\n",
    "\n",
    "\n",
    "'''"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
